#import json
# -*- coding: utf-8 -*-
import os
import codecs
from myCandidates import Candidates



JOKER_CHAR = '*'

default_dic = os.path.join(os.path.dirname(__file__),"dic/spanish.clean") 

#TODO: Read from file
score_dic = {'a':1,'b':3,'c':3,'d':2,'e':1,'f':4,\
                  'g':2,'h':4,'i':1,'j':8,'l':1,'m':3,'n':1,u'ñ':8,'o':1,'p':3,\
                  'q':5,'r':1,'s':1,'t':1,'u':1,'v':4,'y':4,'x':8,'z':10,'*':0}


MAX_RESULT = 40

def checkLetters(w):
    
    
    if len(w) > 9:
        return False
    if len([l for l in w if l not in score_dic]) == 0:
        return True
    else:
        return False;

def isFactible(l,w):
               
        res = sum([w[v] for v in w if w[v]<=l.get(v,0)])
        return  res
            
def CountLetters(w):
    d = {}
    p = 0
    
    for l in w:
        if l in d:
            d[l] += 1
            
        else:
            
            d[l] = 1
            
        if l in score_dic:
                p += score_dic[l]
    return [d,p]

class DicEsp:
    
#    def __init__(self):
#        self.dwords ={}
#        fw = open(defauword = wlt_dic)
#        for w in fw:
#            word = w.strip().replace("\n","").lower()
#            self.dwords[word] = CountLetters(word)
#            
        
    def __init__(self,voc):
        self.dwords ={}
        fw = codecs.open(voc,"r","utf-8")
        
        for w in fw:
            
            word = unicode(w.strip().replace("\n","").lower())
            #word = w.stripno().replace("\n","").lower()
            self.dwords[word] = CountLetters(word)
            
    
            
    
    def returnWords(self,word,excedent=0):
        
        jokers = word.count('*')+excedent
        
        L = CountLetters(word)[0]
        candidates = Candidates(200)
        
        for w in self.dwords:
            
            lw = len(w)
            lword = self.dwords[w]
            
            if  isFactible(L,lword[0])+jokers >= lw and lw > jokers:
                #Candidate [word,score,[letters],
                sortScore = (lw * 1000)+lword[1]
                candidate = [sortScore,w,lword[1],lword[0],getExtraLetters(word,w)]
                candidates.add(candidate)
       
        #candidates.sort(key = lambda w:w[0],reverse = True)
        
        #if len(candidates) > MAX_RESULT: return candidates[:MAX_RESULT]
        return candidates.tolist()

def getExtraLetters(origin,cand):
    return [[l,(l in origin)] for l in cand]
        
#def getExtraLetters(orig, cand):
    
 #   return [l for l in cand if l not in orig]


if __name__ =="__main__":

    #default_dic = "dic/minidic.txt"
    spDic = DicEsp(default_dic)
    
    letters = raw_input()
    
    candidates = spDic.returnWords(letters,0)
    #print json.dumps(candidates)
    print candidates
    for c in candidates:
        print c, getExtraLetters(letters, c[1])

            
